package com.qianfeng.missfood.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.missfood.BaseApp;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.bean.User;
import com.qianfeng.missfood.db.DBHelper;

public class UserMsgActivity extends AppCompatActivity implements View.OnClickListener {

    private RelativeLayout sexSet;
    private TextView sexSelected;
    private EditText nickName;
    private EditText regisPsw;
    private EditText rePsw;
    private String sex;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_msg);


        Intent getInte = getIntent();
        phone = getInte.getStringExtra("phone");
        initView();


    }

    private void initView() {

        ImageView backPressed = (ImageView) findViewById(R.id.pressBack);
        backPressed.setOnClickListener(this);
        nickName = (EditText) findViewById(R.id.nickName);

        sexSet = (RelativeLayout) findViewById(R.id.sexSet);
        sexSet.setOnClickListener(this);

        sexSelected = (TextView) findViewById(R.id.sexSelected);

        regisPsw = (EditText) findViewById(R.id.registerPsw);
        rePsw = (EditText) findViewById(R.id.rePsw);

        Button submit = (Button) findViewById(R.id.registerConfirm);
        submit.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.pressBack:

                break;

            case R.id.sexSet:

                final String[] sexs = new String[]{"男", "女"};
                AlertDialog.Builder builder = new AlertDialog.Builder(this)
                        .setTitle("请选择性别")
                        .setItems(sexs, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                sexSelected.setTextColor(getResources().getColor(R.color.black));
                                sexSelected.setText(sexs[which]);
                                sex = sexs[which];
                                dialog.cancel();
                            }
                        });
                builder.show();

                break;

            case R.id.registerConfirm:

                String name = nickName.getText().toString().trim();
                String password = regisPsw.getText().toString().trim();
                String rePassword = rePsw.getText().toString().trim();

                if ("".equals(name) || !name.matches(".{2,10}")) {

                    Toast.makeText(this, "昵称必须为2-10字的中文或字母数字", Toast.LENGTH_SHORT).show();

                } else if ("请设置".equals(sex)) {

                    Toast.makeText(this, "请选择性别", Toast.LENGTH_SHORT).show();

                } else if ("".equals(password) || !password.matches(".{6,14}")) {

                    Toast.makeText(this, "请设置6-14位密码", Toast.LENGTH_SHORT).show();

                } else if (!password.equals(rePassword)) {

                    Toast.makeText(this, "确认密码必须和登录密码一致", Toast.LENGTH_SHORT).show();

                } else {

                    User user = new User(name, password, sex, phone);
                    Boolean addSucc = DBHelper.DbUser.addUser(user);
                    if (addSucc) {

                        BaseApp.getInstance().setLoginState(true);
                        BaseApp.getInstance().setUser(user);
                        Intent intent = new Intent(this, RegisterSuccessActivity.class);
                        startActivity(intent);
                    } else {

                        Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
                    }

                }

                break;

        }
    }
}
